package day15.atm;

import day9.atm.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon saveImg = new ImageIcon("images/buttonSure.jpg");
    private Font font = new Font("黑体",Font.BOLD,30);
    private JLabel saveLabel1 = new JLabel("请输入原密码：");
    private JLabel saveLabel2 = new JLabel("请输入新密码：");
    private JLabel saveLabel3 = new JLabel("请确认新密码：");
    private JPasswordField accountField1 = new JPasswordField();
    private JPasswordField accountField2 = new JPasswordField();
    private JPasswordField accountField3 = new JPasswordField();
    private JButton saveBtn = new JButton(saveImg);
    public ChangePwdPanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);
        saveLabel1.setBounds(300,200,230,50);
        saveLabel1.setFont(font);
        accountField1.setBounds(530,200,230,50);

        saveLabel2.setBounds(300,250,230,50);
        saveLabel2.setFont(font);
        accountField2.setBounds(530,250,230,50);

        saveLabel3.setBounds(300,310,230,50);
        saveLabel3.setFont(font);
        accountField3.setBounds(530,310,230,50);

        saveBtn.setBounds(442,370,140,50);
        saveBtn.addActionListener(this);
        this.add(saveLabel1);
        this.add(saveLabel2);
        this.add(saveLabel3);
        this.add(accountField1);
        this.add(accountField2);
        this.add(accountField3);
        this.add(saveBtn);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == saveBtn){
            String pwd = new String(accountField1.getPassword());
            SuperArray users = atmFrame.getUsers();
            for (int i = 0;i < users.size();i++){
                User user = (User) users.get(i);
                if (!user.getPwd().equals(pwd)){
                    JOptionPane.showMessageDialog(this,"原密码错误");
                    return;
                }
                while (true){
                    String newpwd = new String(accountField2.getPassword());
                    String newpwd1 = new String(accountField3.getPassword());
                    if (newpwd.equals(newpwd1)){
                        atmFrame.switchPanel("login");
                    }else {
                        JOptionPane.showMessageDialog(this,"两次新密码输入不一致");

                        return;
                    }
                }

            }

        }
    }
}
